const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    labelShow:false,
    shop_id:'',
    talentName:'',
    address:'',
    talentDes:'',
    upImg:[],//图片路径
    upImgId:[],//图片ID
    myShopInfoObj:'',//修改设置的数据
    // 地区信息
    id:'',
    subid:'',
    aresname:'',
    aressubname:'',
    // 行业标签
    taskid: '',
    tasktitle: '',
    tasksubtitle: '',
    shopLabelObj:[],
    shopLabelData:'',
    //毕业院校
    provincelistAll:[],
    province:'',
    city:'',
    provinceName:'',
    provinceShow:false,
    collegeOption:[],
    college:'',
    collegeShow:false,
  },


  onProvince(){
    this.setData({
      provinceShow: true
    })
  },

  provinceCancel(){
    this.setData({
      provinceShow: false
    })
  },

  provinceConfirm(e){
    let res=e.detail.value
    let {province,city,provinceName}=this.data
    province=res[0]
    city=res[1].city
    provinceName=`${province}-${city}`
    this.setData({
      province,
      city,
      provinceName,
      college:'',
    })
    this.getCollege()
    this.provinceCancel()
  },

  onCollege(){
    let {city}=this.data
    if(!city){
      wx.showToast({
        title: '请先选择院校所在地',
        icon: 'none',
        duration: 2000
      })
    }
    this.setData({
      collegeShow: true
    })
  },

  collegeCancel(){
    this.setData({
      collegeShow: false
    })
  },

  collegeConfirm(e){
    console.log(e,'eeeeeeeeeeeeeee')
    let {college}=this.data
    let res=e.detail.value
    college=res.college
    this.setData({
      college,
      collegeShow: false
    })

  },

  provinceChange(event){
    let {provincelistAll}=this.data
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, provincelistAll[value[0]]);
  },

  getProvince(){
    let _this=this
    wx.request({
      url: app.globalData.baseUrl + 'user/setShopProvince',
      data: {
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          let provincelistAll=res.data.data
          let listCode=Object.keys(provincelistAll)
          let provinceLsit=[
            {
              values: listCode,
              className: 'column1',
            },
            {
              values: provincelistAll[listCode[0]],
              className: 'column2',
              defaultIndex: 2,
            },
          ]
          console.log(provinceLsit,'provinceLsit----')
          _this.setData({
            provinceLsit,
            provincelistAll
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  getCollege(){
    let {city,collegeOption}=this.data
    let _this=this
    wx.request({
      url: app.globalData.baseUrl + 'user/setShopCollege',
      data: {
        token: app.globalData.token,
        name:city
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          collegeOption=res.data.data
          _this.setData({
            collegeOption
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  toText(){
    const {talentDes}=this.data
    wx.navigateTo({
      url: '/userPage/pages/storeText/storeText?type=0&text='+talentDes,
    })
  },

  addLabel(){
    this.setData({
      labelShow: true
    })
  },

  onConfirm(event) {
    const {value} = event.detail;
    const {shopLabelObj}=this.data
    let result=shopLabelObj.indexOf(value)
    shopLabelObj.push(value)
    if(result===-1){
      this.setData({
        shopLabelObj
      })
    } else {
      return false
    }
    this.onCancel()
  },

  onCancel() {
    this.setData({
      labelShow: false
    })
  },

  // 店铺名称
  talentNameInp:function(e){
    this.setData({
      talentName: e.detail.value
    })
  },
  // 选择标签
  industryBtn: function () {
    // wx.navigateTo({
    //   url: '/pages/personalLabel/personalLabel?type=1',
    // })
    wx.navigateTo({
      url: '/taskPage/pages/areaList/areaList?type=0',
    })
  },
  // 选择地址
  areaBtn: function () {
    wx.navigateTo({
      url: '/taskPage/pages/areaList/areaList?type=1',
    })
  },
  // 详细地址
  addressInp:function(e){
    this.setData({
      address: e.detail.value
    })
  },
  // 店铺描述
  talentDesInp:function(e){
    this.setData({
      talentDes: e.detail.value
    })
  },
  // 上传图片
  upImgBtn: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.showLoading({
          title: '加载中',
        })
        wx.uploadFile({
          url: app.globalData.baseUrl + 'fileUpload',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            token: app.globalData.token,
            file: tempFilePaths[0]
          },
          success(upres) {
            wx.hideLoading()
            let obj = JSON.parse(upres.data)
            _this.setData({
              upImg: [obj.data.url],
            })
          }
        })
      }
    })
  },
  // 删除图片
  delImg: function (e) {
    var { upImg, upImgId } = this.data;
    var { index } = e.currentTarget.dataset
    upImg.splice(index,1)
    upImgId.splice(index, 1)
    this.setData({
      upImg,
      upImgId
    })
  },
  // 店铺申请提交
  talentSubmit:function(){
    var {provinceName,province, college,city,shop_id , talentName, address, talentDes , id , subid , taskid , upImg,shopLabelObj} = this.data;
    if (talentName == ''){
      wx.showToast({
        title: "请输入店铺名称",
        icon: 'none',
        duration: 2000
      })
      return false;
    };
    if (talentDes == ''){
      wx.showToast({
        title: "请输入店铺描述",
        icon: 'none',
        duration: 2000
      })
      return false;
    };
    if (!provinceName){
      wx.showToast({
        title: "请选择院校所在地",
        icon: 'none',
        duration: 2000
      })
      return false;
    };
    if (!college){
      wx.showToast({
        title: "请选择院校",
        icon: 'none',
        duration: 2000
      })
      return false;
    };
    var data ={
      shop_name:talentName,
      shop_desc:talentDes,
      shop_pic:upImg,
      tags:JSON.stringify(shopLabelObj),
      province,
      city,
      college,
      token: app.globalData.token
    };
    if (shop_id != ''){
      data.id = shop_id;
    }

    wx.request({
      url: app.globalData.baseUrl + 'user/postShopInfoMp',
      data: data,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          wx.showToast({
            title: "设置成功，正在进入店铺",
            icon: 'success',
            duration: 2000
          })
          const pages = getCurrentPages();//获取页面栈
          const beforePage = pages[pages.length - 2];  //前一个页面
          wx.navigateBack({
            delta: 1,
            success: function () {
              //调用前一个页面的方法updateTime()。
              beforePage.myShopChange();
            }

          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.shop_id != undefined){
      var myShopInfoObj = options.myShopInfoObj
      myShopInfoObj = JSON.parse(myShopInfoObj)
      // var upImg = [];
      // var upImgId = [];
      // upImgId.push(myShopInfoObj)
      // upImg.push(myShopInfoObj.shop_pic)
      console.log(myShopInfoObj,'myShopInfoObj----')
      this.setData({
        shop_id: options.shop_id,
        talentName: myShopInfoObj.shop_name,
        shopLabelObj:myShopInfoObj.cate_name,
        province: myShopInfoObj.province,
        city: myShopInfoObj.city,
        college: myShopInfoObj.college,
        provinceName:`${myShopInfoObj.province}-${myShopInfoObj.city}`,
        aresname: myShopInfoObj.province_name,
        aressubname:myShopInfoObj.city_name,
        talentDes: myShopInfoObj.shop_desc,
        upImg: [myShopInfoObj.shop_pic],
      })
    }
    this.shopLabel();
    this.getProvince()
    this.getCollege()
  },
  shopLabel(){
    var _this = this
    wx.request({
      url: app.globalData.baseUrl + 'user/setShopSkills',
      data: {
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          _this.setData({
            shopLabelData:res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  shopLabelChange(e){
    let index = e.detail.value
    let {shopLabelObj,shopLabelData} = this.data
    if(shopLabelObj != ''){
      for(let i = 0;i < shopLabelObj.length;i++){
        if(shopLabelObj[index] == shopLabelObj[i]){
          return false
        }
      }
    }
    shopLabelObj.push(shopLabelData[index])
    this.setData({
      shopLabelObj
    })
  },
  delShopLabel(e){
    let {index} = e.target.dataset
    let {shopLabelObj} = this.data
    shopLabelObj.splice(index,1)
    this.setData({
      shopLabelObj
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
